
source('utils.R')
source('read_dataset.R')

donors = list(
  d12_inds = open_dataset(glue('final_long/cycle=2012')) %>% filter(d_pcc > 0 | d_sp > 0 | d_jfc > 0) %>% pull(component),
  r12_inds = open_dataset(glue('final_long/cycle=2012')) %>% filter(r_pcc > 0 | r_sp > 0 | r_jfc > 0) %>% pull(component),
  d16_inds = open_dataset(glue('final_long/cycle=2016')) %>% filter(d_pcc > 0 | d_sp > 0 | d_jfc > 0) %>% pull(component),
  r16_inds = open_dataset(glue('final_long/cycle=2016')) %>% filter(r_pcc > 0 | r_sp > 0 | r_jfc > 0) %>% pull(component),
  d20_inds = open_dataset(glue('final_long/cycle=2020')) %>% filter(d_pcc > 0 | d_sp > 0 | d_jfc > 0) %>% pull(component),
  r20_inds = open_dataset(glue('final_long/cycle=2020')) %>% filter(r_pcc > 0 | r_sp > 0 | r_jfc > 0) %>% pull(component)
)

d12 = df12[component %chin% donors[['d12_inds']], .(subsq_giver = component %chin% donors[['d16_inds']], nat_quant)]
d12 = d12[, .(sq = mean(subsq_giver)), nat_quant][, class := 'Obama to Clinton'][, cycle := '2012 to 2016']

r12 = df12[component %chin% donors[['r12_inds']], .(subsq_giver = component %chin% donors[['r16_inds']], nat_quant)]
r12 = r12[, .(sq = mean(subsq_giver)), nat_quant][, class := 'Romney to Trump \'16'][, cycle := '2012 to 2016']

d20 = df12[component %chin% donors[['d12_inds']], .(subsq_giver = component %chin% donors[['d20_inds']], nat_quant)]
d20 = d20[, .(sq = mean(subsq_giver)), nat_quant][, class := 'Obama to Biden'][, cycle := '2012 to 2020']

r20 = df12[component %chin% donors[['r12_inds']], .(subsq_giver = component %chin% donors[['r20_inds']], nat_quant)]
r20 = r20[, .(sq = mean(subsq_giver)), nat_quant][, class := 'Romney to Trump \'20'][, cycle := '2012 to 2020']

save(d12, r12, d20, r20, file = 'summary_data/figA7.rda')
